package com.gem.sms.web;

import com.gem.sms.entity.Student;
import com.gem.sms.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/stuInfo")
public class StuInfoController {

    @Autowired
    StudentService
    studentService;

    @RequestMapping("/index")
    public String index()
    {
        return"list";
    }

    @GetMapping("/list")
    public String list(Model model,String pageNow,String searchName)
    {
        List<Student>students = studentService.list();
        if (pageNow == null) {
            //说明是第一次登陆
            pageNow = "1";
        }
        Integer pageNum = Integer.parseInt(pageNow);
        PageInfo<Student> pageBean=studentService.listByPages(pageNum,3,searchName);

        model.addAttribute("pageBean", pageBean);
        return"datas";
    }

    @GetMapping("/delete")
    public String delete(String stuNo)
    {
        studentService.delete(stuNo);
        return "redirect:index";
    }

    @GetMapping("/preEdit")
    public String preEdit(Model model, String stuNo)
    {
        Student student = studentService.getStudentByStuNo(stuNo);
        // 把查询到的产品放入到请求范围
        model.addAttribute("einfo", student);
        return "edit";
    }

    @PostMapping("/edit")
    public String edit(Model model,String stuNo,String name,String password,String birthday,Integer sex,Integer polity,String brief)
    {
        Student student=studentService.getStudentByStuNo(stuNo);
        student.setName(name);
        student.setPassword(password);
        student.setBirthday(LocalDate.parse(birthday));
        student.setSex(sex);
        student.setPolity(polity);
        student.setBrief(brief);
        // 调用业务层
        studentService.updateStu(student);
        return "redirect:index";
    }

    @GetMapping("/checkInfo")
    public String check(Model model,String stuNo)
    {
        Student studnet=studentService.getStudentByStuNo(stuNo);
        model.addAttribute("sinfo",studnet);
        return "stuInfo";
    }

    @GetMapping("/preAdd")
    public String preAdd()
    {
        return "register";
    }

    @PostMapping("/add")
    public String add(Student student)
    {
        studentService.addStu(student);
        return "redirect:index";
    }
}
